package org.jeecg.modules.studentClass.demo.service.impl;

import org.jeecg.modules.studentClass.demo.entity.ClassTable;
import org.jeecg.modules.studentClass.demo.entity.Student;
import org.jeecg.modules.studentClass.demo.mapper.StudentMapper;
import org.jeecg.modules.studentClass.demo.mapper.ClassTableMapper;
import org.jeecg.modules.studentClass.demo.service.IClassTableService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 班级表
 * @Author: jeecg-boot
 * @Date:   2024-07-07
 * @Version: V1.0
 */
@Service
public class ClassTableServiceImpl extends ServiceImpl<ClassTableMapper, ClassTable> implements IClassTableService {

	@Autowired
	private ClassTableMapper classTableMapper;
	@Autowired
	private StudentMapper studentMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ClassTable classTable, List<Student> studentList) {
		classTableMapper.insert(classTable);
		if(studentList!=null && studentList.size()>0) {
			for(Student entity:studentList) {
				//外键设置
				entity.setClassNo(classTable.getClassNo());
				studentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ClassTable classTable,List<Student> studentList) {
		classTableMapper.updateById(classTable);
		
		//1.先删除子表数据
		studentMapper.deleteByMainId(classTable.getId());
		
		//2.子表数据重新插入
		if(studentList!=null && studentList.size()>0) {
			for(Student entity:studentList) {
				//外键设置
				entity.setClassNo(classTable.getClassNo());
				studentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		studentMapper.deleteByMainId(id);
		classTableMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			studentMapper.deleteByMainId(id.toString());
			classTableMapper.deleteById(id);
		}
	}
	
}
